//
//  PlayView.m
//  27_Block练习_播放按钮
//
//  Created by 徐康 on 16/8/19.
//  Copyright © 2016年 KX. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
@interface PlayView ()
@property (nonatomic, strong) UIButton *playBtn;
@property (nonatomic, strong) UIButton *lastBtn;
@property (nonatomic, strong) UIButton *nextBtn;
//@property (nonatomic, copy) void (^play)(UIButton *);
//@property (nonatomic, copy) void (^pause)(UIButton *);
@end
@implementation PlayView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)setup{
    self.playBtn = [self buttonImageWithName:@"player_btn_play_normal" action:@selector(play:)];
    self.lastBtn = [self buttonImageWithName:@"player_btn_pre_normal" action:@selector(last:)];
    self.nextBtn = [self buttonImageWithName:@"player_btn_next_normal" action:@selector(next:)];
    
   // _playBtn.backgroundColor = [UIColor redColor];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _playBtn.frame = CGRectMake(0, 0, 100, 100);
    _playBtn.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    _lastBtn.yj_width = 60;
    _lastBtn.jy_height = 60;
    _lastBtn.yj_centerY = _playBtn.yj_centerY;
    _lastBtn.yj_centerX =_playBtn.yj_centerX -  (_lastBtn.yj_width / 2 + _playBtn.yj_width / 2 + 30);
    _nextBtn.yj_width = 60;
    _nextBtn.jy_height = 60;
    _nextBtn.yj_centerY = _playBtn.yj_centerY;
    _nextBtn.yj_centerX =_playBtn.yj_centerX +  (_lastBtn.yj_width / 2 + _playBtn.yj_width / 2 + 30);
    
}
- (void)play:(Play)play pause:(Play)pause last:(Play)last next:(Play)next {
    self.play = play;
    self.pause = pause;
    self.last = last;
    self.next = next;
}
- (void)play:(UIButton *)button {
    [button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    self.play(button);
}
- (void)pause:(UIButton *)button {
    [button setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(button);
    
}
- (void)last:(UIButton *)button {
    
    self.last(button);
}
- (void)next:(UIButton *)button {
   
    self.next(button);
}
- (UIButton *)buttonImageWithName:(NSString *)name action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [self addSubview:button];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
